package com.tensquare.spit.controller;


import com.tensquare.spit.pojo.Spit;
import com.tensquare.spit.service.SpitService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import enums.ResultEnum;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import util.ResultUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

@RestController
@CrossOrigin
@RequestMapping("/spit")
public class SpitController {

    @Autowired
    private SpitService spitService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private HttpServletRequest request;


    /**
     * 点赞
     *
     * @param id
     * @return
     */
    @PutMapping("/thumbup/{spitId}")
    public Result updateThumbup(@PathVariable("spitId") String id) {

        Claims claims = (Claims) request.getAttribute("user_claims");
        //点赞鉴权
        return Optional.of(claims).map(c -> {
            String userid = c.getId();
            return Optional.ofNullable(redisTemplate.opsForValue().get("thumbup_" + userid + "_" + id))
                    .map(t -> ResultUtil.errorMsg("你已经点过赞了"))
                    .orElseGet(() -> {
                        spitService.updateThumbup(id);
                        redisTemplate.opsForValue().set("thumbup_" + userid + "_" + id, "1");
                        return ResultUtil.successMsg("点赞成功");
                    });
        }).orElse(ResultUtil.valueOf(ResultEnum.ACCESS_ERROR));
    }

    /**
     * 根据上级ID查询吐槽数据
     *
     * @param parentId
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/comment/{parentId}/{page}/{size}")
    public Result findByParentid(@PathVariable String parentId, @PathVariable int page, @PathVariable int size) {
        Page<Spit> pageSpit = spitService.findByParentid(parentId, page, size);
        return ResultUtil.success(new PageResult<Spit>(pageSpit.getTotalElements(), pageSpit.getContent()));
    }

    /**
     * 查询全部数据
     *
     * @return
     */
    @GetMapping
    public Result findAll() {
        return ResultUtil.success(spitService.findAll());
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable String id) {
        return ResultUtil.success(spitService.findById(id));
    }

    /**
     * 发吐槽
     *
     * @param spit
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Spit spit) {
        Claims claims = (Claims) request.getAttribute("user_claims");
        return Optional.ofNullable(claims).map(c -> {
            spit.setUserid(c.getId());
            spit.setNickname(c.getSubject());
            spitService.add(spit);
            return ResultUtil.successMsg("发布吐槽成功！");
        }).orElse(ResultUtil.valueOf(ResultEnum.ACCESS_ERROR));
    }

    /**
     * 修改数据
     *
     * @param spit
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    public Result update(@RequestBody Spit spit, @PathVariable String id) {
        spit.set_id(id);
        spitService.update(spit);
        return ResultUtil.successMsg("修改成功");
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable String id) {
        spitService.deleteById(id);
        return ResultUtil.successMsg("删除成功");
    }


}
